package com.zmn.plat.business.impl.plat.biztype;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.mcc.common.dto.dept.DeptDRO;
import com.zmn.mcc.dubbo.interfaces.dept.DeptListRemoteService;
import com.zmn.plat.business.interfaces.plat.biztype.PlatBizTypeBService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.manager.dictionary.ProductDict;
import com.zmn.plat.model.entity.plat.biztype.PlatBizType;
import com.zmn.plat.model.entity.plat.biztype.log.PlatBizTypeLog;
import com.zmn.plat.services.interfaces.plat.biztype.PlatBizTypeService;
import com.zmn.plat.services.interfaces.plat.biztype.log.PlatBizTypeLogService;

@Service
public class PlatBizTypeBServiceImpl implements PlatBizTypeBService {

    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private DeptListRemoteService deptListRemoteService;

    @Resource
    protected PlatBizTypeService platBiztypeService;

    @Resource
    protected PlatBizTypeLogService platBizTypeLogService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @SuppressWarnings("Duplicates")
    public void modifyPlatBizType(List<PlatBizType> platBizTypes, LoginStaff loginStaff) {
        Date now = DateUtil.getNow();
        String operator = loginStaff.getRealName();
        Integer deptId = loginStaff.getDeptId();
        StringBuffer stringBuffer = new StringBuffer();

        platBiztypeService.deleteAllData();
        platBizTypes.forEach(e -> {
            e.setCreater(operator);
            e.setCreateTime(now);
            e.setUpdater(operator);
            e.setUpdateTime(now);
            stringBuffer.append(ProductDict.getShowPlatName(e.getPlat())).append(":").append(com.zmn.plat.common.dictionary.ProductDict.translateBizTypeEnglishName(e.getBizTypeStr(), "、"))
                .append("，");
        });

        if (CollectionUtil.isNotNullOrEmpty(platBizTypes)) {
            platBiztypeService.insertBatch(platBizTypes);
        }

        if (stringBuffer.length() > 0) {
            PlatBizTypeLog platBizTypeLog = new PlatBizTypeLog();
            platBizTypeLog.setContent(stringBuffer.deleteCharAt(stringBuffer.length() - 1).toString());
            platBizTypeLog.setCreater(operator);
            platBizTypeLog.setCreateTime(now);
            platBizTypeLog.setUpdater(operator);
            platBizTypeLog.setUpdateTime(now);

            ResponseDTO<DeptDRO> data = deptListRemoteService.getDept(deptId);
            DeptDRO dept = new DeptDRO();
            if (data.isSuccess()) {
                dept = data.getData();
            }
            platBizTypeLog.setDeptId(deptId);
            platBizTypeLog.setDeptName(dept.getDeptName());
            platBizTypeLogService.insert(platBizTypeLog);
        }
    }
}
